package com.hqyj.system.service.impl;

import com.hqyj.system.mapper.SysPermissionMapper;
import com.hqyj.system.mapper.SysRolePermissionMapper;
import com.hqyj.system.model.SysPermission;
import com.hqyj.system.model.SysRolePermission;
import com.hqyj.system.service.SysRolePermissionService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: ZLF
 * @Date: 2021/12/30 10:58
 * @Description:
 */
public class SysRolePermissionServiceImpl implements SysRolePermissionService {
    private static SysRolePermissionMapper sysRolePermissionMapper;
    private static SqlSession sqlSession;

    @Override
    public int addSysRolePermissionBySysRolePermission(SysRolePermission sysRolePermission) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysRolePermissionMapper = sqlSession.getMapper(SysRolePermissionMapper.class);
        //############################ 业务 ################################
        int n = sysRolePermissionMapper.insertSelective(sysRolePermission);
        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }

    @Override
    public int deleteSysRolePermissionByRoleId(int roleId) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysRolePermissionMapper = sqlSession.getMapper(SysRolePermissionMapper.class);
        //############################ 业务 ################################
        int n = sysRolePermissionMapper.deleteSysRolePermissionByRoleId(roleId);
        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }
}